package com.bosssoft.g4.aspect;

import com.bosssoft.common.annotation.AutoFill;
import com.bosssoft.common.constant.AutoFillConstant;
import com.bosssoft.common.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * @className: AutoFillAspect
 * 自定义切面，实现公共字段自动填充处理逻辑
 * @author: zbh
 * @date: 2024/8/5 10:20
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    /**
     * 切入点
     * TODO:后续根据实际位置修改
     */
    @Pointcut("execution(* com.bosssoft.g4.mapper.*.*(..)) && " +
            "@annotation(com.bosssoft.common.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     * 前置通知，在通知中进行公共字段的赋值
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){
        log.info("开始进行公共字段自动填充...");
        /**
         * 获取到当前被拦截的方法上的数据库操作类型
         */
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();
        /**
         * 获取到当前被拦截的方法的参数--实体对象
         */
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length == 0){
            return;
        }

        Object entity = args[0];

        //准备赋值的数据
        LocalDateTime now = LocalDateTime.now();
        Long user = 1L;
        Long version = 1L;
        Byte status = 1;
        //根据当前不同的操作类型，为对应的属性通过反射来赋值
        if(operationType == OperationType.INSERT){
            //插入操作为5个公共字段赋值
            try {
                Method setCreatedTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATED_TIME, LocalDateTime.class);
                Method setCreatedUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATED_BY, Long.class);
                Method setUpdatedTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATED_TIME, LocalDateTime.class);
                Method setUpdatedUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATED_BY, Long.class);
                Method setVersion = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_VERSION, Long.class);
                Method setStatus = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_STATUS, Byte.class);
                //通过反射为对象属性赋值
                setCreatedTime.invoke(entity,now);
                setCreatedUser.invoke(entity,user);
                setUpdatedTime.invoke(entity,now);
                setUpdatedUser.invoke(entity,user);
                setVersion.invoke(entity,version);
                setStatus.invoke(entity,status);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(operationType == OperationType.UPDATE){
            //更新操作为2个公共字段赋值
            try {
                Method setUpdatedTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATED_TIME, LocalDateTime.class);
                Method setUpdatedUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATED_BY, Long.class);
                Method setStatus = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_STATUS, Byte.class);
                //通过反射为对象属性赋值
                setUpdatedTime.invoke(entity,now);
                setUpdatedUser.invoke(entity,user);
                setStatus.invoke(entity,status);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
